package cn.sqlexperiment.web.servlet;

import cn.sqlexperiment.web.classes.CaseData;
import cn.sqlexperiment.web.classes.PrescripData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ShowCaseServlet")
public class ShowCaseServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String pageIndex = req.getParameter("pageIndex");
        String Patiname = req.getParameter("Patiname");
        String docno = req.getParameter("docno");
        if (pageIndex == null) {
            pageIndex = "1";
        }
        System.out.println(pageIndex+" "+Patiname+" "+docno);
        List<CaseData> productList = new ArrayList<>();
        Page page = new Page(Integer.valueOf(pageIndex));
        try {
            productList=InSql(page,Patiname,docno);
        } catch (SQLException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        for(CaseData p:productList){
            System.out.println(p.getName()+" "+p.getCaseno()+" "+p.getPatino()+" "+p.getTime()+" "+p.getResult());
        }

        req.setAttribute("page", page);
        req.setAttribute("products", productList);
        req.getRequestDispatcher("showcase.jsp").forward(req, resp);
        resp.setHeader("refresh","0,showcase.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
    public static List<CaseData> InSql(Page page, String Patiname, String docno) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/hospital";
        String sname = "root";
        String password = "12345";
        Connection connection = DriverManager.getConnection(url, sname, password);

        int t= showAll(connection,Patiname,docno);
        /*
        if(t==0){
            page.setTotalPages(1);
            List<CaseData> productList=new ArrayList<>();
            CaseData c = new CaseData();
            c.setPresrcdocno(patino);
            c.setPrescrcaseno(prescr_case_no);
            productList.add(p);
            return productList;
        }*/
        page.setTotalRow(t);
        List<CaseData> productList=selectData(connection,page,Patiname,docno);

        connection.close();
        return productList;
    }
    private static List<CaseData> selectData(Connection connection,Page page,String Patiname, String docno) throws SQLException {
        List<CaseData> productList = new ArrayList<>();
        String sql = "SELECT casea.* FROM casea WHERE case_pati_no=(SELECT patient.pati_no FROM patient WHERE pati_name= ? AND chief_no = ?) ORDER BY look_time ASC limit ?,?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, Patiname);
        statement.setString(2, docno);
        statement.setInt(3, page.getPageStartRow());
        statement.setInt(4, page.getPageSize());
        ResultSet resultSet = statement.executeQuery();
        String pati_name = new String();
        String case_no = new String();
        String case_pati_no = new String();
        String look_time = new String();
        String result = new String();
        while (resultSet.next()) {
            pati_name = resultSet.getString("case_pati_name");
            case_no = resultSet.getString("case_no");
            case_pati_no = resultSet.getString("case_pati_no");
            look_time = resultSet.getString("look_time");
            result = resultSet.getString("rresult");
            CaseData product=new CaseData(case_no,case_pati_no+","+docno,pati_name,look_time,result);
            productList.add(product);
        }
        resultSet.close();
        statement.close();
        return productList;
    }

    private static int showAll(Connection connection,String Patiname, String docno) throws SQLException {
        int t=0;
        String sql = "SELECT casea.* FROM casea WHERE case_pati_no=(SELECT patient.pati_no FROM patient WHERE pati_name= ? AND chief_no = ?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, Patiname);
        statement.setString(2, docno);
        ResultSet resultSet=statement.executeQuery();
        while(resultSet.next()){
            t++;
        }
        statement.close();
        resultSet.close();
        return t;
    }

}
